import os
from moviepy import VideoFileClip

def split_video(input_video, output_folder,chunk_duration=300):
    # 加载视频文件
    video = VideoFileClip(input_video)

    # 获取视频的总时长（秒）
    video_duration = video.duration

    # 按指定时间切割
    chunks = []
    for start_time in range(0, int(video_duration), chunk_duration):
        end_time = min(start_time + chunk_duration, video_duration)
        chunk = video.subclipped(start_time, end_time)
        chunks.append(chunk)

    # 保存所有切割后的片段
    for i, chunk in enumerate(chunks):
        output_file = f"{output_folder}\\{input_video.split('\\')[-1].split('.')[0]}_{i+1}.mp4"
        if os.path.exists(output_file):
            continue
        chunk.write_videofile(output_file, codec="libx264", audio_codec="aac")



ori_folder = 'D:\\bili下载'
output_folder = 'D:\\00_python_auto\\002_split_video\\output_folder'

for cd_i in os.listdir(ori_folder):
    input_video = os.path.join(ori_folder,cd_i)
    split_video(input_video,output_folder, chunk_duration=300)  # 5分钟 = 300秒
